package com.kymatrix.mpo.plus.bpm.server.flowable.candidate.strategy;

import cn.hutool.core.util.StrUtil;
import com.kymatrix.mpo.plus.bpm.server.flowable.candidate.BpmTaskCandidateStrategy;
import com.kymatrix.mpo.plus.bpm.server.flowable.enums.BpmTaskCandidateStrategyEnum;
import com.kymatrix.mpo.plus.system.api.service.DeptApi;
import com.kymatrix.mpo.plus.system.api.dto.DeptmentDto;
import com.kymatrix.mpo.plus.common.util.StrUtils;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.flowable.engine.delegate.DelegateExecution;
import org.springframework.stereotype.Component;

import static com.kymatrix.mpo.plus.common.util.CollectionUtils.convertSet;

/**
 * 部门的负责人 {@link BpmTaskCandidateStrategy} 实现类
 *
 * @author kyle
 */
@Component
public class BpmTaskCandidateDeptLeaderStrategy implements BpmTaskCandidateStrategy {

    @Resource
    private DeptApi deptApi;

    @Override
    public BpmTaskCandidateStrategyEnum getStrategy() {
        return BpmTaskCandidateStrategyEnum.DEPT_LEADER;
    }

    @Override
    public void validateParam(String param) {
        Set<String> deptIds = StrUtils.splitToStringSet(param);
        deptApi.validateDeptList(deptIds);
    }

    @Override
    public Set<String> calculateUsers(DelegateExecution execution, String param) {
        Set<String> deptIds = StrUtil.split(param, ",").stream().collect(Collectors.toSet());
        List<DeptmentDto> depts = deptApi.getDeptList(deptIds);
        return convertSet(depts, DeptmentDto::getLeaderUserId);
    }

}
